//
//  mainTabBarController.m
//  movie
//
//  Created by 李丹阳 on 15/7/17.
//  Copyright (c) 2015年 李丹阳. All rights reserved.
//

#import "mainTabBarController.h"
#import "cinemaViewController.h"
#import "moreViewController.h"
#import "movieViewController.h"
#import "newsViewController.h"
#import "topViewController.h"
#import "mainButton.h"
#import "baseNavigationController.h"

@interface mainTabBarController (){
    UIImageView* selectedImage;
}

@end

@implementation mainTabBarController

- (void)_createView{
    selectedImage = [[UIImageView alloc]initWithFrame:CGRectMake(7, 0, 60, 50)];
    selectedImage.image = [UIImage imageNamed:@"selectTabbar_bg_all1"];
    
    movieViewController* movieView = [[movieViewController alloc]init];
    cinemaViewController* cinemaView = [[cinemaViewController alloc]init];
    moreViewController* moreView = [[moreViewController alloc]init];
    newsViewController* newsView = [[newsViewController alloc]init];
    topViewController* topView = [[topViewController alloc]init];
    
    NSArray* viewArray = [NSArray arrayWithObjects:movieView,newsView,topView,cinemaView,moreView, nil];
    NSMutableArray* navArray = [[NSMutableArray alloc]init];
    for (int i = 0; i<5; i++) {
        baseNavigationController* nav = [[baseNavigationController alloc]initWithRootViewController:viewArray[i]];
        [navArray addObject:nav];
        
    }
    self.viewControllers = navArray;
    
    
}

- (void)_setTabBar{
    Class cla = NSClassFromString(@"UITabBarButton");
    for (UIView* view in self.tabBar.subviews) {
        if ([view isKindOfClass:cla]) {
            [view removeFromSuperview];
        }
    }
}

- (void)addbuttonBottom{
    NSArray* imageNameArray = [NSArray arrayWithObjects:@"movie_home.png",@"msg_new.png",@"start_top250.png",@"icon_cinema.png",@"more_select_setting", nil];
    NSArray* titleName = [NSArray arrayWithObjects:@"电影",@"新闻",@"top",@"影院",@"更多", nil];
    CGFloat high = CGRectGetHeight(self.tabBar.frame);
    CGFloat wide = CGRectGetWidth(self.tabBar.frame)/5;

    [self.tabBar addSubview:selectedImage];
    for (int i = 0; i<5; i++) {
        mainButton* button = [[mainButton alloc]initWithFrame:CGRectMake(i*wide, 0, wide, high)WithBackImage:imageNameArray[i] WithTitle:titleName[i]];
        [self.tabBar addSubview:button];
        button.tag = i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)buttonAction:(mainButton*)btn{
    NSUInteger index = btn.tag;
    self.selectedIndex = index;
    [UIView  beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.2];
    selectedImage.center = btn.center;
    [UIView commitAnimations];
}


- (void)viewDidLoad {

    [super viewDidLoad];
    [self _createView];
    [self _setTabBar];
    [self addbuttonBottom];
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bg_all"];
    self.tabBar.translucent = YES;
    
    //视图添加时 不是最先添加tableView 的时候想要改偏移
    self.tabBar.translucent = YES;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
